package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.PasswordConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.context.BaseContext;
import com.reggie.dto.EmployeeDTO;
import com.reggie.dto.EmployeeLoginDTO;
import com.reggie.dto.EmployeePageQueryDTO;
import com.reggie.dto.PasswordEditDTO;
import com.reggie.entity.Employee;
import com.reggie.exception.BaseException;
import com.reggie.exception.PasswordErrorException;
import com.reggie.mapper.EmployeeMapper;
import com.reggie.result.PageResult;
import com.reggie.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    /**
     *  通过请求中带的参数 获取到 用户名
     *  通过调用mapper中的动态sql方法getByUsername 查询数据库中的用户信息
     */
    public Employee getByUsername(String username) {
        Employee employeemessage = employeeMapper.getByUsername(username);
        return employeemessage;
    }
    //新增用户
    @Override
    public void save(EmployeeDTO employeeDTO) {

        Employee employee = new Employee();
        //属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);
        //设置账户启用/禁用状态
        employee.setStatus(StatusConstant.ENABLE);
        //设置默认密码 转换成md5格式
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes(StandardCharsets.UTF_8)));
   /*     employee.setCreateTime(LocalDateTime.now());//当前创建时间
        employee.setUpdateTime(LocalDateTime.now());//当前修改时间
        //TODO:创建人和修改人 后续解决 解决方案如下
       *//* employee.setCreateUser(100L);
        employee.setUpdateUser(100L);*//*
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());*/
        //调用Mapper的添加方法,将请求到服务器的数据封装后传入insert方法
        employeeMapper.insert(employee);
    }

    /**
     * 根据用户请求的参数 页码 和每页的记录数  分页查询所有用户
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> pageResult = employeeMapper.pageQuery(employeePageQueryDTO);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());

    }


    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        employeeMapper.startOrStopById(status,id);
    }

    /**
     * 通过id查询 对应的员工属性
     * @param id
     * @return
     */
    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        return employee;
    }

    /**
     * 修改对应id员工的属性值
     * @param employeeDTO
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        /*//将浏览器请求带来的参数 复制给employee这个对象
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.update(employee);*/
    }

    /**
     * 修改密码
     * @param passwordEditDTO
     */
    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {
        //具体的处理方法 写在service这里
        //获取新旧密码
        String oldPassword = passwordEditDTO.getOldPassword();
        String newPassword = passwordEditDTO.getNewPassword();
        Long empId = passwordEditDTO.getEmpId(); //获取员工id
        //判断员工的id是否存在 ,如果不存在 主动 把当前线程请求头中的token id取出赋值
        if(empId == null){
           empId = BaseContext.getCurrentId();
        }
        //通过员工id获取员工属性对象
        Employee employee = employeeMapper.getById(empId);
        if(employee == null){
            //如果获取到的员工属性对象是空值 抛出异常
            throw new PasswordErrorException(MessageConstant.PASSWORD_EDIT_FAILED);
        }
        //对新密码进行md5处理
        String Psd = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        //如果请求中输入的旧密码 与 对象获取的密码 不一致 抛出异常
        if(!Psd.equals(employee.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //密码正确就加密新密码  创建返回对象
        Employee emp = Employee.builder().id(empId).password(DigestUtils.md5DigestAsHex(newPassword.getBytes(StandardCharsets.UTF_8))).build();
        employeeMapper.updatePsd(emp);

    }
}
